package com.qingcheng.controller;

import com.aliyun.oss.OSSClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author 岳剑飞
 * @Company http://www.ithiema.com
 */
@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private OSSClient ossClient;
    @Value("${bucketName}")
    private String bucketName;
    @Value("${endpoint}")
    private String endPoint;



    @PostMapping("/oss")
    public String ossUpload(@RequestParam("file")MultipartFile file, String folder){

        String fileName=folder+"/"+ UUID.randomUUID()+file.getOriginalFilename();
        try {
            ossClient.putObject(bucketName,fileName,file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "https://"+bucketName+"."+endPoint+"/"+fileName;
    }


}
